package com.yvon.core.configure;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.yvon.core.support.DefaultMetaObjectHandler;
import com.yvon.core.support.SortPaginationInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Yufeng
 */
@Configuration
@ConditionalOnBean({MybatisPlusProperties.class})
@Slf4j
public class MybatisAutoConfiguration {

    public MybatisAutoConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean({OptimisticLockerInterceptor.class})
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        log.info("mybatis注册乐观锁拦截器..");
        return new OptimisticLockerInterceptor();
    }

    @Bean
    @ConditionalOnMissingBean({MetaObjectHandler.class})
    public MetaObjectHandler metaObjectHandler(MybatisPlusProperties mybatisProperties) {
        log.info("mybatis注册默认填充策略..");
        return new DefaultMetaObjectHandler(mybatisProperties);
    }

    @Bean
    @ConditionalOnMissingBean({PaginationInterceptor.class})
    public PaginationInterceptor paginationInterceptor() {
        log.info("MybatisPlus注册默认分页拦截器...");
        return new SortPaginationInterceptor();
    }
}
